package edu.wy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.wy.common.result.Result;
import edu.wy.dao.EventInformationDao;
import edu.wy.dto.EventInformationDTO;
import edu.wy.entity.EventInformation;
import edu.wy.service.EventInformationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * (EventInformation)表服务实现类
 *
 * @author makejava
 * @since 2025-04-24 16:42:34
 */
@Service("eventInformationService")
@Slf4j
public class EventInformationServiceImpl implements EventInformationService {
    @Resource
    private EventInformationDao eventInformationDao;

    /**
     * 根据用户ID获取list集合
     */
    @Override
    public Result listByUserId(Integer userId) {
        List<EventInformationDTO> eventInformationList = eventInformationDao.queryAllByUserId(userId);
        return Result.ok(eventInformationList);
    }

    /**
     * 根据日期获取当天信息
     * @param userId 当前用户id
     * @return 实例对象
     */
    @Override
    public Result listByDate(Integer userId) {
        Date date = new Date();
        //获取当天年月日
        String year = String.valueOf(date.getYear() + 1900);
        String month = String.valueOf(date.getMonth() + 1);
        String day = String.valueOf(date.getDate());
        String dateStr = year + "-" + month + "-" + day;
        List<EventInformationDTO> eventInformationList = eventInformationDao.queryAllByDate(userId,dateStr);
        return Result.ok(eventInformationList);
    }

    @Override
    public Result listByMonth(Integer userId) {
        Date date = new Date();
        //获取当天年月
        String year = String.valueOf(date.getYear() + 1900);
        String month = String.valueOf(date.getMonth() + 1);
        String dateStr = year + "-" + month;
        List<EventInformationDTO> eventInformationList = eventInformationDao.queryAllByMonth(userId,dateStr);
        return Result.ok(eventInformationList);
    }

    @Override
    public Result listByWeek(Integer userId) {
        LocalDate now = LocalDate.now(ZoneId.systemDefault());
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        // 计算本周的第一天（周一）
        LocalDate beginDay = now.minusDays(dayOfWeek.getValue() - 1);
        // 计算本周的最后一天（周日）
        LocalDate endDay = beginDay.plusDays(6);
        // 将日期转换为字符串格式
        String beginDateStr = beginDay.toString();
        String endDateStr = endDay.toString();
        log.debug("本周的开始日期：{}，结束日期：{}", beginDateStr, endDateStr);
        // 查询本周的事件信息
        List<EventInformationDTO> eventInformationList = eventInformationDao.queryAllByWeek(userId, beginDateStr, endDateStr);
        return Result.ok(eventInformationList);
    }
}
